package com.xiaodou.mjf.application.core.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.core.repository.FinancingLoanRepository;
import com.xiaodou.mjf.application.core.service.FinancingLoanService;
import com.xiaodou.mjf.entity.core.FinancingLoan;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;


/***
 *
 * FinancingLoan业务访问实现
 * @author XiaoDou.com Code Generator
 * @version 1.0
 * 2014-10-29 17:37:52
 *
 */
@Service @Transactional
public class FinancingLoanServiceImpl extends JpaSimpleService<FinancingLoan,String> implements FinancingLoanService{

	Logger logger = LoggerFactory.getLogger(FinancingLoanServiceImpl.class);

    @Resource
    FinancingLoanRepository financingLoanRepository;

    @Override
    protected JpaRepository<FinancingLoan, String> getRepository() {
        return financingLoanRepository;
    }

    public List<FinancingLoan> findByFinancingId(String financingId){
    	return financingLoanRepository.findByFinancingId(financingId);
    }
    
    @Override
    public void deleteByFinancingId(String financingId){
    	financingLoanRepository.deleteByFinancingId(financingId);
    	return;
    }
    
    @Override
	public Page<FinancingLoan> pageDetail(FinancingLoan entity) {
		return financingLoanRepository.findAll(
				(root, query, cb) -> {
					Predicate p1 = cb.equal(
							root.get("financingId").as(String.class),
							entity.getFinancingId());
					Predicate p2 = cb.equal(
							root.get("investStatus").as(String.class),
							entity.getInvestStatus());
					return query.where(p1,p2).getRestriction();
				},
				preparePage(entity, new Sort(new Sort.Order(Sort.Direction.DESC,
						"fullScaleTime"))));
	}

	@Override
	public FinancingLoan findByLoanId(String loanId) {
		return financingLoanRepository.findByLoanId(loanId);
	}
}

